home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / libsipp / sipp_bit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-03  |  3.5 KB  |  164 lines

  1. /**
  2.  ** sipp - SImple Polygon Processor
  3.  **
  4.  **  A general 3d graphic package
  5.  **
  6.  **  Copyright Equivalent Software HB  1992
  7.  **
  8.  ** This program is free software; you can redistribute it and/or modify
  9.  ** it under the terms of the GNU General Public License as published by
  10.  ** the Free Software Foundation; either version 1, or any later version.
  11.  ** This program is distributed in the hope that it will be useful,
  12.  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  ** GNU General Public License for more details.
  15.  ** You can receive a copy of the GNU General Public License from the
  16.  ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  **/
  18.  
  19. /**
  20.  ** sipp_bitmap.c - A sample bitmap for use in SIPP and routines to use it.
  21.  **/
  22.  
  23. #include <sys/types.h>
  24. #include <stdio.h>
  25.  
  26. #include <sipp.h>
  27. #include <smalloc.h>
  28. #include <sipp_bitmap.h>
  29.  
  30.  
  31. /* ================================================================ */
  32.  
  33.  
  34. /*
  35.  * Return a pointer to a new Sipp_bitmap, with the size WIDTH x HEIGHT.
  36.  * Enough space is allocated so each line ends on a byte boundary.
  37.  */
  38. Sipp_bitmap *
  39. sipp_bitmap_create(width, height)
  40.     int   width;
  41.     int   height;
  42. {
  43.     Sipp_bitmap  * bm;
  44.  
  45.     bm = (Sipp_bitmap *) smalloc(sizeof(Sipp_bitmap));
  46.     bm->width = width;
  47.     bm->height = height;
  48.     bm->width_bytes = (width >> 3);
  49.     if ((width & 7) != 0) {
  50.         bm->width_bytes++;
  51.     } 
  52.     bm->buffer = (u_char *) scalloc(bm->width_bytes * height, sizeof(u_char));
  53.  
  54.     return bm;
  55. }
  56.  
  57.  
  58.  
  59. /*
  60.  * Destruct a bitmap, and free allocated memory.
  61.  */
  62. void
  63. sipp_bitmap_destruct(bm)
  64.     Sipp_bitmap  * bm;
  65. {
  66.     if (bm != NULL) {
  67.         if (bm->buffer != NULL) {
  68.             sfree(bm->buffer);
  69.         }
  70.         sfree(bm);
  71.     }
  72. }
  73.  
  74.  
  75.  
  76.  
  77. /*
  78.  * Draw a line in the Sipp_bitmap BM from (X1, Y1)
  79.  * to (X2, Y2)
  80.  */
  81.  
  82. #define PLOT(bm, width_bytes, yres, x, y) \
  83.         (bm)[(y) * (width_bytes) + ((x) >> 3)] |= (1 << (7 - ((x) & 7)))
  84.  
  85. void
  86. sipp_bitmap_line(bm, x1, y1, x2, y2)
  87.     Sipp_bitmap  * bm;
  88.     int            x1, y1;
  89.     int            x2, y2;
  90. {
  91.     int   d;
  92.     int   x,  y;
  93.     int   ax, ay;
  94.     int   sx, sy;
  95.     int   dx, dy;
  96.  
  97.     dx = x2 - x1;
  98.     ax = abs(dx) << 1;
  99.     sx = ((dx < 0) ? -1 : 1);
  100.     dy = y2 - y1;
  101.     ay = abs(dy) << 1;
  102.     sy = ((dy < 0) ? -1 : 1);
  103.  
  104.     x = x1;
  105.     y = y1;
  106.     if (ax > ay) {
  107.         d = ay - (ax >> 1);
  108.         for (;;) {
  109.             PLOT(bm->buffer, bm->width_bytes, bm->height, x, y);
  110.             if (x == x2) {
  111.                 return;
  112.             }
  113.             if (d >= 0) {
  114.                 y += sy;
  115.                 d -= ax;
  116.             }
  117.             x += sx;
  118.             d += ay;
  119.         }
  120.     } else {
  121.         d = ax - (ay >> 1);
  122.         for (;;) {
  123.             PLOT(bm->buffer, bm->width_bytes, bm->height, x, y);
  124.             if (y == y2) {
  125.                 return;
  126.             }
  127.             if (d >= 0) {
  128.                 x += sx;
  129.                 d -=  ay;
  130.             }
  131.             y += sy;
  132.             d += ax;
  133.         }
  134.     }
  135. }
  136.  
  137.  
  138.  
  139. /*
  140.  * Write the Sipp_bitmap BM to the open file FILE.
  141.  */
  142.  
  143. void
  144. sipp_bitmap_write(file, bm)
  145.     FILE         * file;
  146.     Sipp_bitmap  * bm;
  147. {
  148.     int    written;
  149.     int    wrote;
  150.     int    left;
  151.  
  152.     fprintf(file,  "P4\n");
  153.     fprintf(file,  "#Image rendered with SIPP %s\n",  SIPP_VERSION);
  154.     fprintf(file,  "%d\n%d\n", bm->width, bm->height);
  155.  
  156.     wrote   = 0;
  157.     written = 0;
  158.     left    = bm->width_bytes * bm->height;
  159.     while ((wrote = fwrite(bm->buffer + written, 1, left, file)) != left) {
  160.         written += wrote;
  161.         left -= wrote;
  162.     }
  163. }
  164.